package com.zj.study.handle.execption;

import com.zj.study.pojo.RespStatusEnum;
import java.text.MessageFormat;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
public class RespException extends ServiceException {

  public RespException(
      RespStatusEnum status
  ) {
    super(status.getMsg());
    this.respStatusEnum = status;
  }

  public RespException(
      String message,
      RespStatusEnum respStatusEnum
  ) {
    super(message);
    this.respStatusEnum = respStatusEnum;
  }

  public RespException(RespStatusEnum status, String message) {
    super(message);
    this.respStatusEnum = status;
  }

  public RespException(
      RespStatusEnum status,
      String message,
      Object... arguments
  ) {
    super(MessageFormat.format(message, arguments));
    this.respStatusEnum = status;
  }

  public RespException(
      Throwable cause,
      String message,
      RespStatusEnum status
  ) {
    super(cause, message);
    this.respStatusEnum = status;
  }

  /**
   * 响应枚举对象
   */
  private final RespStatusEnum respStatusEnum;


  @Getter
  @AllArgsConstructor
  public enum RespExceptionEnum {
    /**
     * OK：操作成功
     */
    OK(build(RespStatusEnum.OK)),

    /**
     * 客户端
     */
    CLIENT_ERROR(build(RespStatusEnum.CLIENT_ERROR)),
    CLIENT_BAD_PARAMETERS(build(RespStatusEnum.CLIENT_BAD_PARAMETERS)),
    CLIENT_REQUEST_REPEAT(build(RespStatusEnum.CLIENT_REQUEST_REPEAT)),

    /**
     * 系统
     */
    INTERNAL_SERVICE_ERROR(build(RespStatusEnum.INTERNAL_SERVICE_ERROR)),
    INTERNAL_SERVICE_TIME_OUT(build(RespStatusEnum.INTERNAL_SERVICE_TIME_OUT)),
    INTERNAL_SERVICE_DEGRADE(build(RespStatusEnum.INTERNAL_SERVICE_DEGRADE)),
    INTERNAL_SERVICE_RATE_LIMIT(build(RespStatusEnum.INTERNAL_SERVICE_RATE_LIMIT)),
    INTERNAL_SERVICE_FALLBACK(build(RespStatusEnum.INTERNAL_SERVICE_FALLBACK)),
    MIDDLEWARE_ERROR(build(RespStatusEnum.MIDDLEWARE_ERROR)),
    RESOURCE_NOT_FOUND(build(RespStatusEnum.RESOURCE_NOT_FOUND)),
    OPERATION_FORBIDDEN(build(RespStatusEnum.OPERATION_FORBIDDEN)),
    RESOURCE_EXISTED(build(RespStatusEnum.RESOURCE_EXISTED)),
    RESOURCE_CONFLICT(build(RespStatusEnum.RESOURCE_CONFLICT)),


    /**
     * 外部服务
     */
    EXTERNAL_SERVICE_ERROR(build(RespStatusEnum.EXTERNAL_SERVICE_ERROR)),
    EXTERNAL_SERVICE_TIME_OUT(build(RespStatusEnum.EXTERNAL_SERVICE_TIME_OUT)),
    EXTERNAL_SERVICE_DEGRADE(build(RespStatusEnum.EXTERNAL_SERVICE_DEGRADE)),
    EXTERNAL_SERVICE_RATE_LIMIT(build(RespStatusEnum.EXTERNAL_SERVICE_RATE_LIMIT)),
    ;

    private final RespException resp;

    private static RespException build(RespStatusEnum statusEnum) {
      return new RespException(statusEnum);
    }
  }
}